package pl.dalkia.services.scanner.services.reader;

import java.io.BufferedReader;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LogFileReader implements Readable {

	private static final Logger log = LoggerFactory.getLogger(LogFileReader.class);
	
	private BufferedReader bufferedReader;
	
	public LogFileReader(BufferedReader fileBufferedReader){
		bufferedReader = fileBufferedReader;
	}
	
	
	@Override
	public String readLine() {
		String inputLine = getLine();
		if ( inputLine == null ){
			closeConnection();
		}
		return inputLine;
	}

	
	private String getLine(){
		String inputLine = null;
		try {
			inputLine = bufferedReader.readLine();
		} catch (IOException e) {
			log.error("Unable to read from file.");
			System.exit(1);
		} 
		return inputLine;
	}
	
	
	private void closeConnection(){
		try {
			bufferedReader.close();
		} catch (IOException e) {
			log.error("Unable to close file properly.");
			System.exit(1);
		}
	}
	
}
